package com.atguigu.yygh.user.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.user.utils.IpUtils;
import com.atguigu.yygh.utils.AuthContextHolder;
import com.atguigu.yygh.vo.acl.UserQueryVo;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author DongYu Zhou
 * @date 2022/6/29 21:39
 */
@Api(tags = "用户登录接口")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户登录接口
     *
     * @param loginVo 接受实体类vo
     * @param request 接收id地址
     * @return 返回map 放入name 和 token信息
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        //获取ip
        String ipAddr = IpUtils.getIpAddr(request);
        loginVo.setIp(ipAddr);
        Map<String, Object> map = userInfoService.login(loginVo);
        return R.ok().data(map);
    }

    //用户认证接口
    @ApiOperation(value = "用户认证接口")
    @PostMapping("auth/userAuth")
    public R userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //获取用户id
        Long userId = AuthContextHolder.getUserId(request);
        //保存
        userInfoService.saveUserInfo(userId, userAuthVo);
        return R.ok();
    }


    //获取用户信息接口 翻译字段并返回
    @ApiOperation(value = "获取用户信息接口")
    @GetMapping("auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {
        //获取用户id
        Long userId = AuthContextHolder.getUserId(request);
//        查询
        UserInfo userInfo = userInfoService.getUserInfo(userId);
        return R.ok().data("userInfo", userInfo);
    }
}

